<?php

namespace app\client\controller;

use app\common\Auth;
use app\common\model\Commond;
use app\common\model\Msg;
use think\Container;
use think\Controller;
use think\facade\Log;

class Client extends Controller
{
    
// 你的 APPID AK SK
    const APP_ID = '11376431';
    const API_KEY = '9dMWSmjLeBsdAADUGN4igUNE';
    const SECRET_KEY = 'lzAUeq4LFj1FwgErWbOGmrtua06PQDR6';
    private  function get_cmd($str,$cmds){
        if(is_array($str)){
            $str = $str[0];
        }
        $items = str_split($str);
        $pow = [];
        foreach($cmds as $k=>$cmd){
            $pow[$k] = 0;
            foreach($items as $item){
                if(strpos($cmd->keyword,$item)!==false){
                    $pow[$k]++;
                }
            }
        }
        $key = array_search(max($pow),$pow);
        Log::info(  "+_+_+_+_+_+_+_+计算get_cmd+_+_+_+_+_+_+_+_+" );
        Log::info(  $str );
        Log::info(  $cmds );
        Log::info(  $key );
        Log::info(  $pow );
        Log::info(  "+_+_+_+_+_+_+_+计算get_cmd+_+_+_+_+_+_+_+_+" );
        return  $cmds[$key] === 0?[]:$cmds[$key];
    }
    private function client(){
        require_once Container::get('app')->getRootPath().'/extend/baiduAI/AipSpeech.php';
        $client = new \AipSpeech(self::APP_ID, self::API_KEY, self::SECRET_KEY);
        return $client;
    }
    public function read($msg = "hello 主人"){
        $client = self::client();
        $result = $client->synthesis($msg, 'zh', 1, array(
            'vol' => 5,
            'per'=>4,
            'spd'=>4
        ));
        return $result;
    }
    public function speech(){
        $client = self::client();
        $audio = $this->upload();
        if($audio){
            $data = $client->asr(file_get_contents($audio), 'amr', 8000);
            if(isset($data["err_no"]) && $data["err_no"] == 0){
                $result = $data["result"];//文字
                $cmds = Commond::userCommond(Auth::getUserId());//指令集
                $cmd = $this->get_cmd($result,$cmds);
                if(isset($cmd->id)){ //获取指令
                    Msg::send($cmd);//发送指令
                    return json([
                        "code"=>1000,
                        "msg"=>"已处理".$cmd->title,
                        "data"=>$data
                    ]);
                }
            }
            return json([
                "code"=>1002,
                "msg"=>"没理解您的意思",
                "data"=>$data
            ]);
        }
        return json([
            "code"=>1001,
            "msg"=>"没听清主人说的话",
        ]);
    }
    private function upload(){
        $file = request()->file('audio');
        // 移动到框架应用根目录/public/uploads/ 目录下
        if($file){
            $server_file_path_ext = 'public' . "/" . 'uploads';
            $server_file_path = Container::get('app')->getRootPath() .$server_file_path_ext;
            $info = $file->move($server_file_path);
            if($info){
                $path = $server_file_path_ext  . "/" .  $info->getSaveName();
                $ser_path = $server_file_path  . "/" .  $info->getSaveName();
                return $ser_path;
            }else{
                // 上传失败获取错误信息
                $resp = $file->getError();
                return false;
            }
        }
    }

    function index(){
        return view();
    }
}
